package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    /**
     *  根据名称分页查询频道列表
     * @param dto
     * @return
     */
//    @Transactional
    @Override
    public ResponseResult findByNamdAndPage(ChannelDTO dto) {
        //校验参数
        if (dto==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查分页参数是否正常
        dto.checkParam();
        //封装查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.<AdChannel>lambdaQuery();
        //频道名称(判断非空）
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(AdChannel::getName,dto.getName());
        }
        //状态（判断参数状态能为空
        if (dto.getStatus()!=null) {
            queryWrapper.eq(AdChannel::getStatus,dto.getStatus());
        }
        //序号升序
        queryWrapper.orderByAsc(AdChannel::getOrd);
        //创建分页对象
        Page<AdChannel> pageReq = new Page<>();
        //执行查询
        IPage<AdChannel> pageResult = this.page(pageReq, queryWrapper);
        //封装响应结果
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal(), pageResult.getRecords());
        return pageResponseResult;
    }

    /**
     *  新增
     * @param channel
     * @return
     */
    @Transactional
    @Override
    public ResponseResult insert(AdChannel channel) {
        //参数校验判断对象是否为空，或者频道对象的名字是否为空
        if (channel==null|| StringUtils.isBlank(channel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断该频道是否存在
        int count = this.count(Wrappers.<AdChannel>lambdaQuery()
                .eq(AdChannel::getName,channel.getName()));
        if (count>0) {
            //如果大于0，则说明频道存在，抛出异常信息
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //执行新增
        channel.setCreatedTime(new Date());
        save(channel);
        return ResponseResult.okResult();
    }

    /**
     * 修改
     * @param adChannel
     * @return
     */
    @Transactional
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1.数据校验
        if (adChannel==null||adChannel.getId()==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.通过id查询数据，判断数据是否存在
        AdChannel channel = getById(adChannel.getId());
        if (channel==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道信息不存在");
        }
        //3.校验名称唯一性
        if (StringUtils.isNotBlank(adChannel.getName())&&!adChannel.getName().equals(channel.getName())) {
            int count = this.count(Wrappers.<AdChannel>lambdaQuery()
                    .eq(AdChannel::getName,adChannel.getName()));
            if (count>0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在");
            }
        }
        //执行修改
        updateById(adChannel);
        //4.返回结果
        return ResponseResult.okResult();
    }

    @Transactional
    @Override
    public ResponseResult deleteById(Integer id) {
        //1.检查参数
        if (id==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断当前频道是否存在
        AdChannel channel = getById(id);
        if (channel==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //启用状态下不能删除
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //3.删除频道
//        int i=1/0;
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
